package com.cqq.login.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.cqq.auth.service.bean.MenuBean;
import com.cqq.common.core.constant.SessionConstant;

@Controller(value = "indexController")
@RequestMapping(value = { "/" })
@SessionAttributes(SessionConstant.MENUS)
public class IndexController {

	@RequestMapping(value = { "index" })
	public String index(HttpServletRequest request, @ModelAttribute(SessionConstant.MENUS) List<MenuBean> menus) {
		String url = "/userMange/showUserInfo.htm";
		MenuBean menu = getMenuBean(menus);
		if(menu!=null&&menu.getUrl()!=null)
			url = menu.getUrl();
		return "redirect:" + url;
	}

	private MenuBean getMenuBean(List<MenuBean> menus){
		if (!CollectionUtils.isNotEmpty(menus))
			return null;
		if(menus.get(0).getUrl()!=null)
			return menus.get(0);
		else
			return getMenuBean(menus.get(0).getChildren());
	}
	
}